<template>
 <el-container>
     <!-- 头部区域 -->
  <el-header>
    <div class="header_logo">
      <img src="../assets/首页.png" alt="">
      <span>后台管理系统</span>
    </div>
    <el-button type="info" @click="userOut">退出</el-button>
  </el-header>
    <!-- 主题区域 -->
    <el-container>
    <!-- 右侧侧边栏 -->
    <el-aside :width="isToggle?'64px':'200px'">
      <!-- 左侧侧边栏菜单区域 -->
      <!-- 折叠按钮 -->
      <div class="toggle-button" @click="isToggle=!isToggle">☰</div> 
      <el-menu
      background-color="#545c64"
      text-color="#fff"
      active-text-color="#84bf96"
      unique-opened
      :collapse="isToggle"
      :router="true"
      :default-active="activePath"
      >
      <!-- 一级菜单 -->
      <el-submenu :index="item.id.toString()" v-for="item in menuList" :key="item.id">
        <!-- 一级菜单模板 -->
        <template slot="title">
          <!-- 图标 -->
          <i :class="icons[item.id]"></i>
          <!-- 文本 -->
          <span>{{item.authName}}</span>
        </template>
        <!-- 二级菜单 -->
        <el-menu-item :index="`/${subItem.path}`" v-for="subItem in item.children" :key="subItem.id"
        @click="savaNav(`/${subItem.path}`)">
          <template slot="title">
            <i class="el-icon-s-unfold"></i>
           <span>{{subItem.authName}}</span> 
          </template>
        </el-menu-item>
      </el-submenu>
    </el-menu>
    </el-aside>
    <!-- 左侧内容区 -->
    <el-main>
      <router-view></router-view>
    </el-main>
  </el-container>
</el-container>
</template>

<script>
export default {
      created(){
        this.getMenuList();
        this.activePath=window.sessionStorage.getItem('activePath')
      },
      name:'home',
      data(){
          return{
          menuList:[],
          // 图标数据
          icons:{
            '125':'iconfont icon-user',
            '103':'iconfont icon-tijikongjian',
            '101':'iconfont icon-shangpin',
            '102':'iconfont icon-danju',
            '145':'iconfont icon-baobiao',
          },
          isToggle:false,
          // 被激活的地址
          activePath:'',
          }
      },
      methods:{
          userOut(){
              window.sessionStorage.clear();
              this.$router.push('/login');
              this.$message.warning('退出登录');
          },
         async getMenuList(){
          //  接受并简化返回的promise值
            const{data,}=await this.$http.get('menus')
            if(data.meta.status !== 200) return this.$message.error(data.meta.msg)
            // console.log(data.data);
            this.menuList=data.data;
          },
          savaNav(activePath){
            window.sessionStorage.setItem('activePath',activePath);
            this.activePath=activePath
          },
      }
}
</script>

<style lang="less" scoped>
.el-container{
  width: 100%;
  height: 100%;
}
.el-header{
  background-color:rgb(84, 92, 100);
  display: flex;
  justify-content: space-between;
  padding: 0;
  color: white;
  .el-button{
    margin: 10px 15px 10px 0 ;
  }
}
.el-aside{
  background-color:rgb(84, 92, 100);
  overflow: hidden;
  .el-menu{
    width: 100%;
  }
  
}
.el-main{
  background-color: rgb(255,255,255);
  position: relative;
}
.header_logo{
  display: flex;
  align-items: center;
  img{
    margin-right: 15px;
    height: 59px;
    width: 59px;
  }
}
.toggle-button{
  background-color: #4e72b8;
  font-size: 20px;
  text-align: center;
  line-height: 35px;
  height: 35px;
  color: white;
  cursor: pointer;
}
</style>>

